﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class CreateEvent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Member"] == null)
            {
                Session["flag"] = "redirecterror";
                Response.Redirect("~/registration.aspx");
            }

            string s = Session["Member"].ToString();
            //check here
            rvStartDate.MinimumValue = DateTime.Now.ToShortDateString();
            rvStartDate.MaximumValue = DateTime.Now.AddYears(5).ToShortDateString();
            rvEndDate.MinimumValue = DateTime.Now.ToShortDateString();
            rvEndDate.MaximumValue = DateTime.Now.AddYears(5).ToShortDateString();
        }

        protected void btnCreateEvent_Click(object sender, EventArgs e)
        {
            DateTime startTime = GetTime(tbxSHour.Text, tbxSMinute.Text, tbxSAMPM.Text, Convert.ToDateTime(tbxCreateSdate.Text).Year, Convert.ToDateTime(tbxCreateSdate.Text).Month, Convert.ToDateTime(tbxCreateSdate.Text).Day);

            DateTime endTime = GetTime(tbxEHour.Text, tbxEMinute.Text, tbxEAMPM.Text, Convert.ToDateTime(tbxCreateEdate.Text).Year, Convert.ToDateTime(tbxCreateEdate.Text).Month, Convert.ToDateTime(tbxCreateEdate.Text).Day);

            //***Need to throw up the error so we know we encountered ex, so we can redirect or show notifications to user

            
                string eventId = EventControl.CreateEvent(Session["Member"].ToString(),tbxCreateTitle.Text, tbxCreateDesc.Text, startTime, endTime, Convert.ToDouble(tbxCreateFee.Text),Convert.ToInt32( ddlCreateCat.SelectedValue ), tbxCreateLocation.Text, Session["Member"].ToString(), rdblCreatePrivacy.SelectedIndex);

                if (eventId != null)
                {
                    if (fuCreatePoster.HasFile)
                    {
                        Utility.UploadEventPoster(fuCreatePoster, eventId);
                    }
                    else 
                    {
                        Utility.UploadDefaultPoster(eventId);
                    }

                    if (fuCreateThumbnail.HasFile)
                    {
                        Utility.UploadEventThumbnail(fuCreateThumbnail, eventId);
                    }
                    else 
                    {
                        Utility.UploadDefaultEventThumbnail(eventId);
                    }
                    
                    

                    //Utility.UploadEventPoster(Server, fuCreatePoster, eventId);
                    //Utility.UploadEventThumbnail(Server, fuCreateThumbnail, eventId);

                    //created msg
                    //redirect to event...
                    Session["flag"] = "created";
                    Response.Redirect("~/ViewEvent.aspx?eventid=" + eventId);
                }
                else
                {
                    //failed msg
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! One of more required fields were leave empty!');", true);
                }

        }

        DateTime GetTime(string hour, string minute, string ampm, int year, int month, int day)
        {
            int modifier = 0;

            switch (ampm)
            {
                case "AM":
                    modifier = 0;
                    break;
                case "PM":
                    modifier = 12;
                    break;
            }


            return new DateTime(year, month, day, Convert.ToInt32(hour) + modifier, Convert.ToInt32(minute), 0);
        }


    }
}